<?php


namespace App\Console\Commands;


use Aitels\Foundation\Annotation\ApplicationAnnotationsContainer;
use Aitels\Foundation\Annotation\ConfigAnnotation;
use Aitels\Foundation\Annotation\ListeningAnnotation;
use Aitels\Foundation\Annotation\RouteAnnotation;
use Aitels\Foundation\Annotation\ScheduleAnnotation;
use Aitels\Foundation\Scanner\Scanner;
use Illuminate\Console\Command;

class Deploy extends Command
{
    protected $signature = 'deploy';
    protected $description = '缓存注解';

    public function handle()
    {
        $this->scanAnnotationThenCache();
        $this->call('config:cache');
        $this->call('route:cache');
    }


    protected function scanAnnotationThenCache()
    {
        //实例化扫描器，扫描目录
        $scanner = new Scanner([
            base_path('modules/web'),
            base_path('modules/admin'),
            base_path('modules/h5'),
            base_path('modules/common')
        ]);

        ApplicationAnnotationsContainer::setCachePath(storage_path('annotation/cache.php'));
        //实例化一个应用注解容器
        $annotationContainer = new ApplicationAnnotationsContainer(
            ListeningAnnotation::class,//事件注解
            RouteAnnotation::class,//路由注解
            ScheduleAnnotation::class,//定时任务注解
            ConfigAnnotation::class//config注解
        );
        //注册文件扫描回调函数
        $scanner->registerHandler(function (\SplFileInfo $fileInfo) use ($annotationContainer) {
            $annotationContainer->handle($fileInfo);
        });
        //开始扫描
        $scanner->scan();
        //每次扫描后缓存扫描结果，关闭ANNOTATION_SCAN后直接读缓存
        ApplicationAnnotationsContainer::cacheAnnotation();
    }
}
